package com.lntea.jdbc.dbutils;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.commons.dbutils.AsyncQueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class AsyncQueryRunnerDemo {
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		ResultSetHandler<List<User>> rsh = new BeanListHandler<User>(User.class);
		AsyncQueryRunner asyncQueryRunner = new AsyncQueryRunner(Executors.newCachedThreadPool());
		
		Future<List<User>> future = asyncQueryRunner.query(ConnectionUtil.getConnection(),  "select * from user", rsh);
		
		List<User> users = null;
		try {
			users = future.get(5, TimeUnit.SECONDS);
		} catch (InterruptedException | ExecutionException | TimeoutException e) {
			e.printStackTrace();
		}
		
		for(int i=0;i<users.size();i++){
			System.out.println(users.get(i));
		}
	}
}
